﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Airborne.ScrumPoker.Model.Cards;
using Airborne.ScrumPoker.Module.Events;

namespace Airborne.ScrumPoker.Module.Developer.Views
{
    /// <summary>
    /// Interaction logic for CardsView.xaml
    /// </summary>
    public partial class CardsView : UserControl, ICardsView
    {
        private ICard[] CardSet{get; set;}

        public CardsView()
        {
            InitializeComponent();
        }

        public bool VotingEnabled
        {
            get
            {
                return cardDeck.IsEnabled;
            }
            set
            {
                cardDeck.IsEnabled = value;

            }
        }

        #region CardSelected

        public event EventHandler<GenericEventArgs<ICard>> CardSelected;

        private void RaiseCardSelected(ICard card)
        {
            GenericEventArgs<ICard> args = new GenericEventArgs<ICard>(card);
            OnCardSelected(args);
        }

        protected virtual void OnCardSelected(GenericEventArgs<ICard> args)
        {
            EventHandler<GenericEventArgs<ICard>> handler = this.CardSelected;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        public void SetSequence(ICard[] sequence)
        {
            CardSet = sequence;
            LoadCards(CardSet);
        }

        private void LoadCards(ICard[] cards)
        {
            cardDeck.Children.Clear();

            double controlHeight = 136;
            double controleWidth = 100;

            // Create a name scope for the page.
            NameScope.SetNameScope(this, new NameScope());


            double destinationvPoint = 0;
            int controlPosition = 0;

            int controlPadding = 12;

            for (int i = 0; i < cards.Length; i++)
            {

                ICard card = cards[i];
                GroupBox groupBox = new GroupBox() {  Background = Brushes.Red, Name = "groupBox" + i.ToString(CultureInfo.InvariantCulture), Visibility = Visibility.Hidden };

                groupBox.Template = this.Resources["flipItemTemplate"] as ControlTemplate;
                groupBox.DataContext = card;
                groupBox.Margin = new Thickness(20, 20, 20, 20);  

                this.RegisterName(groupBox.Name, groupBox);


                double destinationhPoint = (controleWidth + controlPadding) * controlPosition ;
                if ((destinationhPoint + controleWidth) > cardDeck.ActualWidth + (controlPadding * 3))
                {
                    destinationvPoint = controlHeight + controlPadding + 40;
                    destinationhPoint = 0;
                    controlPosition = 0;
                }
                controlPosition++;

                ObjectAnimationUsingKeyFrames visibility = new ObjectAnimationUsingKeyFrames();

                visibility.KeyFrames.Add(new DiscreteObjectKeyFrame(Visibility.Visible, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.1 * i))));

                DoubleAnimation vMove = new DoubleAnimation(0, destinationvPoint, new Duration(TimeSpan.FromSeconds(0.2)));
                DoubleAnimation hMove = new DoubleAnimation(0, destinationhPoint, new Duration(TimeSpan.FromSeconds(0.2)));


                Storyboard.SetTargetName(hMove, groupBox.Name);
                Storyboard.SetTargetProperty(hMove, new PropertyPath(Canvas.LeftProperty));

                Storyboard.SetTargetName(vMove, groupBox.Name);
                Storyboard.SetTargetProperty(vMove, new PropertyPath(Canvas.TopProperty));

                Storyboard.SetTargetName(visibility, groupBox.Name);
                Storyboard.SetTargetProperty(visibility, new PropertyPath(Rectangle.VisibilityProperty));

                cardDeck.Children.Add(groupBox);

                Storyboard storyBoard = new Storyboard();
                storyBoard.Children.Add(hMove);
                storyBoard.Children.Add(vMove);
                storyBoard.Children.Add(visibility);
                storyBoard.BeginTime = TimeSpan.FromSeconds(0.4);
                storyBoard.Begin(groupBox);
            }
        }


        private void groupBoxCardSelected(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement control = sender as FrameworkElement;
            if (control != null)
            {
                ICard selectedCard = control.DataContext as ICard;
                if (selectedCard != null)
                {
                    ResetCardSelection();
                    selectedCard.IsSelected = true;
                    RaiseCardSelected(selectedCard);
                }
            }
        }

        private void ResetCardSelection()
        {
            foreach (ICard card in CardSet)
            {
                card.IsSelected = false;
            }
        }

    }
}
